package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
菜单管理
*/
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;


    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(String username) {
        try {
            List<Menu> firstMenuList = menuService.findMenuByUsername(username);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,firstMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //查询所有一级菜单
    @RequestMapping("/queryAllFirstMenu")
    public Result queryAllFirstMenu() {
        try {
            List<Menu> firstMenuList = menuService.queryAllFirstMenu();
            return new Result(true, MessageConstant.QUERY_QUERYALLFIRSTMENU_SUCCESS,firstMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUERYALLFIRSTMENU_FAIL);
        }
    }

    //查询所有一级菜单
    @RequestMapping("/findChildrensByParentId")
    public Result findChildrensByParentId(Integer id) {
        try {
            List<Menu> childrenMenuList = menuService.findChildrensByParentId(id);
            return new Result(true, MessageConstant.QUERY_QUERYALLFIRSTMENU_SUCCESS,childrenMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUERYALLFIRSTMENU_FAIL);
        }
    }

    //查询所有子级菜单
    @RequestMapping("/queryChildrenMenu")
    public Result queryChildrenMenu() {
        try {
            List<Menu> childrenMenuList = menuService.queryChildrenMenu();
            return new Result(true, MessageConstant.QUERY_QUERYALLFIRSTMENU_SUCCESS,childrenMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_QUERYALLFIRSTMENU_FAIL);
        }
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
//            menu.setCreateDate(new Date());
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }


    //    分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }


    //删除
    @RequestMapping("/del")
//    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")  //权限校验
    public Result del(Integer id) {
        try {
            menuService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    //回显菜单
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //编辑数据存储
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    //    查询所有菜单信息
    @RequestMapping("/FindAll")
    public Result find() {
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //    查询所有菜单信息
    @RequestMapping("/getUserMenu")
    public Result getUserMenu(String username) {
        try {
            List<Menu> userMenuList = menuService.getUserMenu(username);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, userMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
